{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using device cpu\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'data/tokenizer_vocab.json'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 18\u001b[0m\n\u001b[0;32m     15\u001b[0m     DEVICE \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmps\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     16\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUsing device \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mDEVICE\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 18\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m CLIPTokenizer(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata/tokenizer_vocab.json\u001b[39m\u001b[38;5;124m\"\u001b[39m, \n\u001b[0;32m     19\u001b[0m                           merges_file\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata/tokenizer_merges.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m, clean_up_tokenization_spaces\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m     20\u001b[0m model_file_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata/v1-5-pruned-emaonly.ckpt\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     21\u001b[0m models \u001b[38;5;241m=\u001b[39m model_loader\u001b[38;5;241m.\u001b[39mpreload_models_from_standard_weights(model_file_path, DEVICE)\n",
      "File \u001b[1;32mc:\\mynvironment\\anaconda3\\Lib\\site-packages\\transformers\\models\\clip\\tokenization_clip.py:306\u001b[0m, in \u001b[0;36mCLIPTokenizer.__init__\u001b[1;34m(self, vocab_file, merges_file, errors, unk_token, bos_token, eos_token, pad_token, **kwargs)\u001b[0m\n\u001b[0;32m    303\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnlp \u001b[38;5;241m=\u001b[39m BasicTokenizer(strip_accents\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, do_split_on_punc\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m    304\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfix_text \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m--> 306\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(vocab_file, encoding\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mutf-8\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m vocab_handle:\n\u001b[0;32m    307\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mencoder \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mload(vocab_handle)\n\u001b[0;32m    308\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdecoder \u001b[38;5;241m=\u001b[39m {v: k \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mencoder\u001b[38;5;241m.\u001b[39mitems()}\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'data/tokenizer_vocab.json'"
     ]
    }
   ],
   "source": [
    "import model_loader\n",
    "import pipline\n",
    "from PIL import Image\n",
    "from transformers import CLIPTokenizer\n",
    "import torch\n",
    "\n",
    "DEVICE = \"cpu\"\n",
    "ALLOW_CUDA = False\n",
    "ALLOW_MPS = False\n",
    "\n",
    "if torch.cuda.is_available() and ALLOW_CUDA:\n",
    "    DEVICE = \"cuda\"\n",
    "\n",
    "elif torch.backends.mps.is_available() and ALLOW_MPS:\n",
    "    DEVICE = \"mps\"\n",
    "print(f\"Using device {DEVICE}\")\n",
    "\n",
    "tokenizer = CLIPTokenizer(\"data/tokenizer_vocab.json\", \n",
    "                          merges_file=\"data/tokenizer_merges.txt\", clean_up_tokenization_spaces=False)\n",
    "model_file_path = \"data/v1-5-pruned-emaonly.ckpt\"\n",
    "models = model_loader.preload_models_from_standard_weights(model_file_path, DEVICE)\n",
    "\n",
    "\n",
    "#Text to Image\n",
    "prompt = \"A beautiful chinese girl, 1080p resolution\"\n",
    "uncond_prompt = \"\" #负面提示\n",
    "do_cfg = True\n",
    "cfg_scale = 7\n",
    "\n",
    "#Image to Image\n",
    "input_image = None\n",
    "image_path = \"images/test.jpg\"\n",
    "#input_image = Image.open(image_path)\n",
    "strength = 0.9\n",
    "\n",
    "sampler = \"ddpm\"\n",
    "num_inference_steps = 50\n",
    "seed = 42\n",
    "\n",
    "output_image = pipline.generate(\n",
    "    prompt=prompt, uncond_prompt=uncond_prompt, \n",
    "    input_image=input_image, strength=strength, do_cfg=do_cfg, cfg_scale=cfg_scale, \n",
    "    sample_name=sampler, inference_steps=num_inference_steps, seed=seed, \n",
    "    models=models, device=DEVICE, idle_device=\"cpu\", tokenizer=tokenizer\n",
    ")\n",
    "\n",
    "Image.fromarray(output_image)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
